<?php
class Acl_user_model extends CI_Model {
	
	private $tbl_data= 'ACL_USER_LOGIN';
	private $tbl_data2= 'ACL_ROLES';
	private $tbl_data3= 'ACL_USER_ROLES';
	private $tbl_data4= 'ACL_USER_CORPORATE';
	public function __construct()
	{
		parent::__construct();
	}
	
	function list_all($username,$firstname){
                $sql= "SELECT A.USERNAME,A.FIRST_NAME,A.LAST_NAME,A.LAST_LOGIN,A.STATUS FROM ACL_USER_LOGIN A where STATUS='Enabled' ";
		$username=str_replace("'", "''",$username);
                $firstname=str_replace("'", "''",$firstname);
		if ($username) $sql.=" and username like '$username%'";
                if ($firstname) $sql.=" and first_name like '$firstname%'";
		return $sql;
	}
	
	function count_all(){
		return $this->db->count_all($this->tbl_data);
	}
 
	function get_by_id($id){
		$this->db->where('USERNAME', $id);
		return $this->db->get($this->tbl_data)->row();
	 
	}

	function get_all_role(){
		return $this->db->get($this->tbl_data2)->result_array();	 	   	      
	}
	
	function get_corporate_user($username){
		$this->db->where('USERNAME', $username);
		return $this->db->get($this->tbl_data4)->result_array();	 	   	      
	}
	
	function get_role_user($username){
		$this->db->where('USERNAME', $username);
		return $this->db->get($this->tbl_data3)->result_array();	 	   	      
	}
	
	function save($itmes){
		$this->db->insert($this->tbl_data, $itmes);
	}
	
	function save_role($itmes){
		$this->db->insert($this->tbl_data3, $itmes);
	}
	
	function save_corporate($itmes){
		$this->db->insert($this->tbl_data4, $itmes);
	}
	
	function update($items,$id){
		$this->db->where('USERNAME', $id);
		$this->db->update($this->tbl_data, $items);
	}
	
	function delete($id){
		$this->delete_corporate_by_user($id);
		$this->delete_role_by_user($id);
		$this->db->where('USERNAME', $id);
		$this->db->delete($this->tbl_data);
	}
	
	function delete_role_by_user($id){
		$this->db->where('USERNAME', $id);
		$this->db->delete($this->tbl_data3);
	}
	
	function delete_corporate_by_user($id){
		$this->db->where('USERNAME', $id);
		$this->db->delete($this->tbl_data4);
	}
	
 
}
?>